import 'package:flutter/material.dart';
import 'package:ad_x_sdk_example/com/qlslylq/flutterdesign/scene/BaseScene.dart';
import 'package:flutter/services.dart';
import '../constant/KeyConstant.dart';
import '../util/Log.dart';
import 'dart:convert';

/*
 * 横幅广告测试主页 <br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class BannerAdTestMainScene extends BaseScene {
  State<StatefulWidget> createState() {
    return new BannerAdTestMainSceneState();
  }
}

/*
 * 页面功能 <br/>
 */
class BannerAdTestMainSceneState extends BaseSceneState {
  EventChannel eventChannel = EventChannel('com.qlslylq.ad-x-sdk/adListener');
  var channel;

  BannerAdTestMainSceneState() {
    title = '横幅广告测试';
  }

  void initState() {
    super.initState();
    eventChannel.receiveBroadcastStream().listen((dynamic event) {
      String eventName = event['eventName'];
      if (eventName == "onAdShow") {
        Log.dt("onAdShow");
      } else if (eventName == "onAdShowError") {
        var adError = event['error'];
        Log.dt("onAdShowError:" + jsonEncode(adError).toString());
      } else if (eventName == "onAdDismiss") {
        Log.dt("onAdDismiss");
      } else if (eventName == "onAdClick") {
        Log.dt("onAdClick");
      }
    }, onError: (dynamic error) {}, cancelOnError: true);
  }

  void dispose() async {
    super.dispose();
    if (channel != null) {
      await channel.invokeMethod("closeAd");
      await channel.invokeMethod("destroyAd");
    }
  }

  Widget build(BuildContext context) {
    var listTileHeight = 33.5;
    var listTileMargin = EdgeInsets.fromLTRB(50, 0, 50, 0);

    double adContainerHeightDp = 90;
    int adContainerHeight =
        (adContainerHeightDp * MediaQuery.of(context).devicePixelRatio).toInt();

    return buildBody(
        context,
        new ConstrainedBox(
            constraints: BoxConstraints.expand(),
            child: new Stack(children: <Widget>[
              new Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    new Container(
                      width: MediaQuery.of(context).size.width,
                      height: listTileHeight,
                      margin: listTileMargin,
                      child: new RaisedButton(
                        onPressed: () async {
                          if (channel != null) {
                            await channel.invokeMethod("closeAd");
                            await channel.invokeMethod("destroyAd");
                          }
                        },
                        child: new Text('销毁广告'),
                        color: Theme.of(context).buttonColor,
                        shape: Border.all(
                            color: Colors.teal,
                            style: BorderStyle.solid,
                            width: 1),
                      ),
                    ),
                    new Container(
                      width: MediaQuery.of(context).size.width,
                      height: listTileHeight,
                      margin: listTileMargin,
                      child: new RaisedButton(
                        onPressed: () {
                          showToast("正在开发中~");
                        },
                        child: new Text('其它功能'),
                        color: Theme.of(context).buttonColor,
                        shape: Border.all(
                            color: Colors.yellow,
                            style: BorderStyle.solid,
                            width: 1),
                      ),
                    )
                  ]),
              new Positioned(
                  width: MediaQuery.of(context).size.width,
                  height: adContainerHeightDp,
                  bottom: 0,
                  child: new AndroidView(
                    viewType: "com.qlslylq.ad-x-sdk/bannerAdView",
                    creationParams: {
                      "JH_BANNER_POS_ID": KeyConstant.JH_BANNER_POS_ID,
                      "w": 0,
                      "h": adContainerHeight,
                      "gravity": 80 | 1
                    },
                    creationParamsCodec: const StandardMessageCodec(),
                    onPlatformViewCreated: (int id) async {
                      var params = {
                        "slideIntervalTime": 30 * 1000,
                      };

                      channel = new MethodChannel(
                          "com.qlslylq.ad-x-sdk/bannerAdView_$id");
                      await channel.invokeMethod("initLoader");
                      await channel.invokeMethod(
                          "setSlideIntervalTime", params);
                      await channel.invokeMethod("loadAndShow");
                    },
                  )),
            ])));
  }

  void onClick(Widget widget) {}
}
